package com.chuan.it_market.service.impl;

import com.chuan.it_market.dao.GoodsDao;
import com.chuan.it_market.dao.SellerDao;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Goods;
import com.chuan.it_market.pojo.entity.Seller;
import com.chuan.it_market.pojo.vo.GoodsVO;
import com.chuan.it_market.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    GoodsDao goodsDao;
    @Resource
    SellerDao sellerDao;
    @Override
    public ResponseDTO add(Goods goods) {

        int i=goodsDao.insertSelective(goods);
        if(i!=0){
            return ResponseDTO.ok("商品添加成功");
        }
      else return ResponseDTO.fail("商品添加失败");
    }


    @Override
    public ResponseDTO getGoodsByTnoAndSno(Integer typeno, Integer sno) {
        List<Goods> goods=goodsDao.selectByTnoAndSno(typeno,sno);
        return ResponseDTO.ok("查询成功",goods);
    }

    @Override
    public List<Goods> selectAllGoods(Integer sno) {
        return goodsDao.selectAllGoods(sno);
    }

    @Override
    public List<Goods> getGoodsByInput(String name,Integer sno) {

        return goodsDao.getGoodsByInput(name,sno);
    }

    @Override
    public Goods getGoodsDetail(Integer gno) {
        return goodsDao.selectByPrimaryKey(gno);
    }

    @Override
    public Goods getGoodsByPrimaryKey(Integer sno) {
        return goodsDao.selectByPrimaryKey(sno);
    }

    @Override
    public ResponseDTO update(Goods goods) {
        int i =goodsDao.updateByPrimaryKeySelective(goods);
        if(i!=0){return ResponseDTO.ok("修改成功");}
        else{
            return ResponseDTO.fail("修改失败");
        }
    }

    @Override
    public int delete(Integer gno) {
        return goodsDao.deleteByPrimaryKey(gno);
    }

    @Override
    public GoodsVO getGoodsVODtetail(Integer gno) {
        GoodsVO goodsVO=goodsDao.selectGoodsVOByPrimaryKey(gno);
        Seller seller=sellerDao.selectByPrimaryKey(goodsVO.getSno());
        goodsVO.setSeller(seller);
        return goodsVO;
    }

    @Override
    public List<GoodsVO> getGoodsByInput2(String name) {
        List<GoodsVO> goodsVOS= goodsDao.getGoodsByInput2(name);
        for(GoodsVO s:goodsVOS)
        {
            Seller seller= sellerDao.selectByPrimaryKey(s.getSno());
            s.setSeller(seller);
        }
        return goodsVOS;
    }

    @Override
    public List<GoodsVO> getGoodsVOSByTno(Integer typeno) {
        List<GoodsVO> goodsVOS= goodsDao.selectByTno(typeno);
        for(GoodsVO s:goodsVOS)
        {
            Seller seller= sellerDao.selectByPrimaryKey(s.getSno());
            s.setSeller(seller);
        }
        return goodsVOS;
    }

    @Override
    public void ReCount(Goods goods) {
        goodsDao.updateByPrimaryKeySelective(goods);
    }

    @Override
    public List<GoodsVO> selectAllGoods(){
        List<GoodsVO> goodsVos= goodsDao.selectAllGoods2();
        for(GoodsVO s:goodsVos)
       {
         Seller seller= sellerDao.selectByPrimaryKey(s.getSno());
         s.setSeller(seller);
       }
        return goodsVos;
    }
}
